﻿/* Copyright 2008-10 Jarrett Vance
 * see http://www.codeplex.com/blogsvc/license */
namespace AtomSite.WebCore
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;

  /// <summary>
  /// This attribute extends the HandleErrorAttribute to use our ErrorModel
  /// that is compatible with the master page.
  /// </summary>
  public class HandleSiteErrorAttribute : System.Web.Mvc.HandleErrorAttribute
  {
    public override void OnException(System.Web.Mvc.ExceptionContext filterContext)
    {
      base.OnException(filterContext);

      //just update the model to use our base page model
      ViewResult vr = filterContext.Result as ViewResult;
      if (vr != null)
      {
        HandleErrorInfo error = vr.ViewData.Model as HandleErrorInfo;
        if (error != null)
        {
          ErrorModel model = new ErrorModel() { HandleErrorInfo = error };
          BaseController bc = filterContext.Controller as BaseController;
          if (bc != null)
          {
            bc.SetModel(model);
            vr.ViewData = new ViewDataDictionary<ErrorModel>(model);
          }
        }
      }
    }
  }
}
